import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/login'
import * as types from '@/vuex/types'
import store from '@/vuex'
Vue.use(Router)
// 页面刷新时，重新赋值token
if (window.localStorage.getItem('token')) {
    store.commit('login/'+types.LOGIN, window.localStorage.getItem('token'))
}
const router = new Router({
  mode:'history',
  routes: [
    // {
    //   path: '/:id([A-Z]{3})',
    //   name: 'HelloWorld',
    //   component:function(resolve){
    //     require(['@/components/HelloWorld'], resolve)
    //   } 
    // },
    {
      path: '/maoxiu',
      name: 'maoxiu',
      component:function(resolve){
        require(['@/components/maoxiu'], resolve)
      } 
    },
    {
      path:'/mx/:openid',
      component:function(resolve){
        require(['@/components/listView'], resolve)
      }
    },
    {
      path:'*',
      component:function(resolve){
        require(['@/components/NoFound'], resolve)
      }
    },
    {
        path: '/repository',
        name: 'repository',
        meta: {
            requireAuth: true,
        },
        component:function(resolve){
          require(['@/components/repository'], resolve)
        }
    },
    {
      path:'/repository',
      alias:['/edit'],
    },
    {
        path: '/login/:openid',
        name: '登录',
        component:function(resolve){
          require(['@/components/login'], resolve)
        }
    },    
    {
        path: '/childLogin/:openid',
        name: '子账号登录',
        component:function(resolve){
          require(['@/components/childLogin'], resolve)
        }
    },
    {
      path: '/listView/:openid',
      name: '列表页',
      component:function(resolve){
        require(['@/components/listView'], resolve)
      }
    },
    {
      path: '/chooseModel/:status',
      name: '模板选择',
      component:function(resolve){
        require(['@/components/chooseModule'], resolve)
      }
    },
    {
      path: '/Models0_1/:id/:uid/:username',
      name: 'Models0_1',
      component:function(resolve){
        require(['@/components/module0_1'], resolve)
      },
    },
  ]
});
// simple history management
const history = window.sessionStorage
history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0)
let isPush = false
let endTime = Date.now()
let methods = ['push', 'go', 'replace', 'forward', 'back']
document.addEventListener('touchend', () => {
  endTime = Date.now()
})
methods.forEach(key => {
  let method = router[key].bind(router)
  router[key] = function (...args) {
    isPush = true
    method.apply(null, args)
  }
})
router.beforeEach((to, from, next) => {
  store.commit('loading/updateLoadingStatus', {isLoading: true})
  const toIndex = history.getItem(to.path)
  const fromIndex = history.getItem(from.path)
  if (toIndex) {
    if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
      store.commit('loading/updateDirection', {direction: 'forward'})
    } else {
      // 判断是否是ios左滑返回
      if (!isPush && (Date.now() - endTime) < 377) {
        store.commit('loading/updateDirection', {direction: ''})
      } else {
        store.commit('loading/updateDirection', { direction: 'reverse' })
      }
    }
  } else {
    ++historyCount
    history.setItem('count', historyCount)
    to.path !== '/' && history.setItem(to.path, historyCount)
    store.commit('loading/updateDirection', {direction: 'forward'})
  }
  //console.log(toIndex)
    if (to.matched.some(r => r.meta.requireAuth)) {
        if (store.state.login.token) {
            next();
        }else {
          next({
              path: '/login',
              query: {redirect: to.fullPath}
          })
        }
    }else {
      next();
    }
})
router.afterEach(function (to) {
  store.commit('loading/updateLoadingStatus', {isLoading: false})
  if (process.env.NODE_ENV === 'development') {
    ga && ga('set', 'page', to.fullPath)
    ga && ga('send', 'pageview')
  }
})
export default router
